package com.example.administrator.base.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import android.view.View;
import android.widget.TextView;

import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.bean.GprsReqBean;
import com.example.administrator.base.R;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.SharedPreferencesUtils;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class LockMessage extends BaseActivity {
    @InjectView(R.id.Toolbar)
    android.support.v7.widget.Toolbar Toolbar;
    @InjectView(R.id.tv_number)
    TextView tvNumber;
    @InjectView(R.id.tv_lasttime)
    TextView tvLasttime;
    @InjectView(R.id.tv_star)
    TextView tvStar;
    @InjectView(R.id.tv_barrty)
    TextView tvBarrty;
    @InjectView(R.id.tv_message)
    TextView tvMessage;
    SweetAlertDialog sweetAlertDialog;
    String bicyclenum = "";
    @InjectView(R.id.tv_speed)
    TextView tvSpeed;
    @InjectView(R.id.tv_voltage)
    TextView tvVoltage;
    @InjectView(R.id.tv_online)
    TextView tvOnline;
    @InjectView(R.id.tv_locationtime)
    TextView tvLocationtime;
    @InjectView(R.id.tv_last_change_time)
    TextView mTvLastChangeTime;
    @InjectView(R.id.tv_isChanging)
    TextView mTvIsChanging;
    @InjectView(R.id.tv_simno)
    TextView tvSimno;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock_message);
        ButterKnife.inject(this);
        Toolbar.setTitle("赳猎人");
        Toolbar.setSubtitle("车锁详情");
        setSupportActionBar(Toolbar);
        Toolbar.setNavigationIcon(R.drawable.backbar);
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        sweetAlertDialog = new SweetAlertDialog(this, 5);
        sweetAlertDialog.setCanceledOnTouchOutside(false);
        sweetAlertDialog.setTitleText("加载中");
        Intent intent = getIntent();
        bicyclenum = intent.getStringExtra("car");
        if (!bicyclenum.equals("")) {
            tvNumber.setText(bicyclenum);
            GetcarMessage(bicyclenum);
        } else {
            showToast("数据异常请返回重试");
        }
    }

    @Override
    protected void onResponseSuccess(Response response, String action) {
        ResponseBody body = response.body();
        if (body != null) {
            try {
                String string = body.string();
                final GprsReqBean mse = GsonTools.changeGsonToBean(string, GprsReqBean.class);
                if (mse == null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showToast("该车信息不完整，请用蓝牙查看");
                            sweetAlertDialog.dismiss();
                        }
                    });
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                if (mse.getCode() == 1) {
                                    showToast("" + mse.getMessage());
                                    tvLasttime.setText(getString(R.string.updatatime, mse.getLockGpsRealData().getMap().getUpdateTime()));
                                    tvStar.setText(getString(R.string.starnumber, mse.getLockGpsRealData().getMap().getSatelliteNum()));
                                    tvBarrty.setText(getString(R.string.gprsv, mse.getLockGpsRealData().getMap().getBatteryLevel()));
                                    tvMessage.setText(getString(R.string.carmessage, mse.getLockGpsRealData().getMap().getLockStatus()));
                                    tvSpeed.setText(getString(R.string.signelqd, mse.getLockGpsRealData().getMap().getSignalIntensity()));
                                    tvVoltage.setText(getString(R.string.gprsya, mse.getLockGpsRealData().getMap().getBatteryVoltage()));
                                    tvLocationtime.setText(getString(R.string.lastlocationtime, mse.getLockGpsRealData().getMap().getSendTime()));
                                    tvSimno.setText(getString(R.string.simno, mse.getLockGpsRealData().getMap().getSimNo()));
                                    if (mse.getLockGpsRealData().getMap().getOnline()) {
                                        tvOnline.setText("是否在线：是");
                                    } else {
                                        tvOnline.setText("是否在线：否");
                                    }
                                    if (mse.getLockGpsRealData().getMap().getIsCharging() == 0) {
                                        mTvIsChanging.setText("是否充过电:未充过");
                                    } else {

                                        mTvIsChanging.setText("是否充过电:有充过");
                                    }
                                    if (TextUtils.isEmpty(mse.getLockGpsRealData().getMap().getLastChargeTime())) {
                                        mTvLastChangeTime.setText("最后一次充电时间:未找到充电记录");
                                    } else {
                                        mTvLastChangeTime.setText(getString(R.string.lastcdtime, mse.getLockGpsRealData().getMap().getLastChargeTime()));
                                    }

                                } else {
                                    showToast("" + mse.getMessage());
                                    tvLasttime.setText(mse.getMessage());
                                    tvStar.setText(mse.getMessage());
                                    tvBarrty.setText(mse.getMessage());
                                    tvMessage.setText(mse.getMessage());
                                    tvSpeed.setText(mse.getMessage());
                                    tvVoltage.setText(mse.getMessage());
                                    tvOnline.setText(mse.getMessage());
                                    tvLocationtime.setText(mse.getMessage());
                                    mTvLastChangeTime.setText(mse.getMessage());
                                    mTvIsChanging.setText(mse.getMessage());
                                    tvSimno.setText(mse.getMessage());

                                }
                            } catch (Exception e) {
                                showToast("该车数据不完整，请使用蓝牙查看");
                                sweetAlertDialog.dismiss();
                                e.printStackTrace();
                            }

                        }
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showToast("数据不完整，请重试");
                    sweetAlertDialog.dismiss();
                }
            });
        }

    }

    @Override
    protected void onResponseFailure(Throwable e, String action) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast("网络异常，请重试");
                sweetAlertDialog.dismiss();
            }
        });
    }

    /*获取车锁详情*/
    public void GetcarMessage(String number) {
        String id = String.valueOf(SharedPreferencesUtils.getParam(LockMessage.this, "adminId", ""));
        RequestBody formBody = new FormBody.Builder()
                .add("action", "GETBIKEREALDATA")
                .add("adminId", id)
                .add("bicycleNo", number)
                .build();
        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/operationperson?")
                .post(formBody)
                .build();
        postRequest(request, "GETBIKEREALDATA");

    }
}
